package cn.leyaber.hrm.quartz;

import org.quartz.*;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration //是spring一个配置文件
public class QuartzConfig {

    //为quartz创建一个自己数据源
    @Bean
    @QuartzDataSource
    @ConfigurationProperties(prefix = "spring.quartz.properties.org.quartz.datasource")
    DataSource quartzDataSource(){
        return DataSourceBuilder.create().build();
    }

     //创建商品详情
    @Bean
    public JobDetail newJob(){
        return JobBuilder.newJob(PrintTimeJob.class)
                .withIdentity("myJob")//给商品取唯一标示
                //.usingJobData(Map)
                .usingJobData("xxx","zzz")
                .storeDurably()////即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
    }

    //给商品详情创建触发器
    @Bean
    public Trigger printTimeJobTrigger(){
        //调度器
        CronScheduleBuilder schedule = CronScheduleBuilder.cronSchedule("0/1 * * * * ? ");
        return TriggerBuilder.newTrigger()
                .forJob(newJob())
                .withIdentity("printTimeJobTrigger")
                .withSchedule(schedule)
                .build();
    }

}
